﻿namespace NetFx.SMIL.Utils
{
  using System;
  using System.Collections.Generic;
  using System.Text;

  public class FullName
  {
    #region Statics
    public static readonly FullName EmptyName = new FullName("", "","");
    public static FullName of(string _namespace, string _typename,string _instance)
    {
      return new FullName(_namespace, _typename, _instance);
    }
    public static FullName of(Identifier namespaceId, Identifier typenameId,Identifier instanceId)
    {
      return new FullName(namespaceId, typenameId, instanceId);
    }
    #endregion
    #region Fields
    protected Identifier _namespace = null;
    protected Identifier _typename = null;
    protected Identifier _instance = null;
    #endregion
    #region Properties
    public virtual Identifier NamespaceNameIdentifier
    {
      get
      {
        return this._namespace == null?
          this._namespace = Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator,"")
          :this._namespace;
      }
      set
      {
        this._namespace = value == null?
          Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator,"")
          :value;
      }
    }
    public virtual Identifier TypeNameIdentifier
    {
      get
      {
        return this._typename == null ?
          this._typename = Identifier.CreateIdentifierOf(Identifier.NestingClassSeparator, "")
          : this._typename;
      }
      set
      {
        this._typename = value == null ?
          Identifier.CreateIdentifierOf(Identifier.NestingClassSeparator, "")
          : value;
      }

    }
    public virtual Identifier InstanceNameIdentifier
    {
      get
      {
        return this._instance == null ?
          this._instance = Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator, "")
          : this._instance;
      }
      set
      {
        this._instance = value == null ?
          Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator, "")
          : value;
      }
    }
    public virtual string NamespaceName
    {
      get
      {
        return this.NamespaceNameIdentifier.ToString();
      }
      set
      {
        this.NamespaceNameIdentifier = (value!=null)?
          Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator,value):
          Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator, "");

      }
    }
    public virtual string TypeName
    {
      get
      {
        return this.TypeNameIdentifier.ToString();
      }
      set
      {
        this.TypeNameIdentifier = (value!=null)? 
          Identifier.CreateIdentifierOf(Identifier.NestingClassSeparator,value):
          Identifier.CreateIdentifierOf(Identifier.NestingClassSeparator, "");
      }
    }
    public virtual string InstanceName
    {
      get
      {
        return this.InstanceNameIdentifier.ToString();
      }
      set
      {
        this.InstanceNameIdentifier = (value != null) ?
          Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator, value) :
          Identifier.CreateIdentifierOf(Identifier.NamespaceSeparator, "");
      }
    }
    public virtual bool HasNamespaceName
    {
      get
      {
        return this.NamespaceNameIdentifier.NameExists;
      }
    }
    public virtual bool HasTypeName
    {
      get
      {
        return this.TypeNameIdentifier.NameExists;
      }
    }
    public virtual bool HasInstanceName
    {
      get
      {
        return this.InstanceNameIdentifier.NameExists;
      }
    }
    public virtual string FullTypeName
    {
      get
      {
        return (this.HasNamespaceName ? this.NamespaceName + Identifier.NamespaceSeparator : string.Empty)
          + (this.HasTypeName ? this.TypeName + Identifier.NamespaceSeparator : string.Empty);
      }
    }
    public virtual string FullInstanceName
    {
      get
      {
        return this.FullTypeName + this.InstanceName;
      }
    }
    #endregion
    #region Constructors
    public FullName(string _namespace, string _typename,string _instance)
    {
      this.NamespaceName = _namespace;
      this.TypeName = _typename;
      this.InstanceName = _instance;
    }
    public FullName(Identifier namespaceId,Identifier typenameId,Identifier instanceId)
    {
      this.NamespaceNameIdentifier = namespaceId;
      this.TypeNameIdentifier = typenameId;
      this.InstanceNameIdentifier = instanceId;
    }
    #endregion
    #region Methods
    public override string ToString()
    {
      return this.FullInstanceName;
    }
    public override bool Equals(object obj)
    {
      if (obj is FullName)
      {
        return this.FullInstanceName == (obj as FullName).FullInstanceName;
      }
      return false;
    }
    public override int GetHashCode()
    {
      return this.FullInstanceName.GetHashCode();
    }
    #endregion
  }
}
